<script src="<?=base_url()?>templates/js/tabui.js" type="text/javascript"></script>
<script src="<?=base_url()?>templates/js/price_format.js" type="text/javascript"></script>
<script src="<?=base_url()?>templates/js/datetimepicker_css.js" type="text/javascript"></script>
<script type="text/javascript" src="<?=base_url()?>templates/js/jquery.formatCurrencyVN.js"></script>
<script src="<?=base_url()?>templates/js/price.js" type="text/javascript"></script>
<script language="javascript">    
jQuery(document).ready(function(){
	//themanh();
});
</script>
<?=form_open('product/addproduct/',array('id'=>'adminformproduct'));?>

<input type="hidden" value="detailed" id="selected_section" name="selected_section">
<div class="tabs cm-j-tabs cm-track">
   <ul>
      <li class="cm-js cm-active" id="detailed"><a>Tổng thể</a></li>
      <li class="cm-js" id="images"><a>Hình ảnh</a></li>
   </ul>
</div>
<div class="cm-tabs-content">
   <div id="content_detailed" class="hidden">
      <h2 class="subheader">Thông tin</h2>
      <table width="100%" class="form">
         <tr>
            <td width="260" class="required label">Mã SP</td>
            <td><input type="text" id="ProductCode" class="w150" name="ProductCode" value=""></td>
         </tr>
         <tr>
            <td class="required label">Tên sản phẩm</td>
            <td><input type="text" id="ProductName" class="max" name="ProductName" value=""></td>
         </tr>
         <tr>
            <td class="required label">Giá</td>
            <td><input type="text" class="w150" id="Price" name="Price" value=""  onblur="FPrice.set_priceHCM();">
                <span id="Price_label"></span>
                <input name="VAT" type="checkbox" id="VAT" value="1" checked="checked" />
                VAT</td>
            </tr>
         <tr>
            <td class="required label">Danh mục sản phẩm</td>
            <td><select name="CategoryID">
                  <? foreach($listcat as $cat):
                        $listsub1 = $this->product->check_parentid($cat->CategoryID);
                            ?>
                  <option value="<?=$cat->CategoryID?>">
                  <?=$cat->CategoryName?>
                  </option>
                  <? if(count($listsub1) > 0){?>
                  <? foreach($listsub1 as $sub1):
						$listsub2 = $this->product->check_parentid($sub1->CategoryID);
					?>
                  <option value="<?=$sub1->CategoryID?>">¦&nbsp;&nbsp;
					  <?=$cat->CategoryName?>
                      &nbsp;¦&nbsp;
                      <?=$sub1->CategoryName?>
                  </option>
                  <? if(count($listsub2) > 0){?>
                  <? foreach($listsub2 as $sub2):?>
                  <option value="<?=$sub2->CategoryID?>">¦&nbsp;&nbsp;
					  <?=$cat->CategoryName?>
                      &nbsp;¦&nbsp;
                      <?=$sub1->CategoryName?>
                      ¦&nbsp;
                      <?=$sub2->CategoryName?>
                  </option>
                  <?  //start level 3
						$listsub3 = $this->product->check_parentid($sub2->CategoryID);
						if(count($listsub3) > 0){
							foreach($listsub3 as $sub3):?>
								<option value="<?=$sub3->CategoryID?>">¦&nbsp;&nbsp;
								<?=$cat->CategoryName?>
								&nbsp;¦&nbsp;
								<?=$sub1->CategoryName?>
								¦&nbsp;
								<?=$sub2->CategoryName?>
								¦&nbsp;
								<?=$sub3->CategoryName?>
								</option>
						<? endforeach;?>
						<? } //end level 3 ?>
                  <? endforeach;?>
                  <? }?>
                  <? endforeach?>
                  <? }?>
                  <? endforeach;?>
               </select></td>
         </tr>
         <tr>
            <td class="label">Tính năng</td>
            <td><textarea id="wysiwyg-advanced" name="ProductFunc"></textarea></td>
         </tr>
         <tr>
             <td class="required label">Hiển thị trang chủ</td>
             <td><input type="checkbox" name="IsHome" value="1"></td>
         </tr>
         <tr>
             <td class="required label">Hiển thị</td>
             <td><input type="checkbox" name="bl_active" value="1" checked="checked"></td>
         </tr>
      </table>
      <h2 class="subheader">Hiện trạng sản phẩm</h2>
      <table width="100%" class="form">
         <tr>
             <td width="182" class="required label">Kích thước</td>
             <td><input type="text" class="w100" name="ProductSize" value="">
                ví dụ: 20x20</td>
         </tr>
         <tr>
             <td class="required label">Trọng lượng</td>
             <td><input type="text" class="w100" name="ProductWeight" value="">
                KG</td>
         </tr>
         <tr>
             <td class="required label">Hiện trạng</td>
             <td><input type="radio" checked="checked" value="1" id="ProductStatus" name="ProductStatus">
                 Còn hàng
                 <input type="radio" value="2" id="ProductStatus" name="ProductStatus">
                 Hết hàng
                 <input type="radio" value="3" id="ProductStatus" name="ProductStatus">
                 Có thể đặt hàng<br />
                 <input type="radio" value="4" id="ProductStatus" name="ProductStatus">
                 Tùy chọn
                <input type="text" name="ProductStatusText" value="" class="w400"></td>
         </tr>
        </table>
   </div>
   <div id="content_images" class="hidden">
      <h2 class="subheader">Hình ảnh</h2>
      <table width="100%" class="form">
         <tr>
            <td class="label">Hình chính</td>
            <td><div id="main_img"><img src="<?=base_url_site()?>uploads/no_image.gif" alt=""></div>
               <input type="hidden" name="ProductImg" value="" id="ProductImg"></td>
         </tr>
         <tr>
            <td class="label">Hình ảnh</td>
            <td><div id="upload_button" class="  "><span>Chọn File Upload<span></span></span></div>
               <div id="status_message">Click chọn hình để đưa vào hình chỉnh của sản phẩm</div>
               <ul id="list_img">
                  <? foreach($listimg as $val):?>
                  <li class="success">
                     <div  align="center"><img width="85" height="85" src="<?=base_url_site()?>uploads/images/<?=$val->ImagePath?>" onclick="chosen(<?=$val->ImageID?>)"> 
                     <a class="delete_button" id="<?=$val->ImageID?>" href="javascript:;"> Xóa</a><span style="display: none;" id="v<?=$val->ImageID?>"><?=$val->ImagePath?></span></div>
                  </li>
                  <? endforeach;?>
               </ul></td>
         </tr>
      </table>
   </div>
</div>
<input type="submit" name="bt_submit" value="" class="themmoi">
<?=form_close();?>
<script type="text/javascript">
     $("#adminform").validate({
        rules: {
            ProductName: "required",
            Alias: "required",
        },
        messages: {
            ProductName: "Vui lòng điền tên sản phẩm",
            Alias: "Mã trên link không được để trống.",
        } 
    });
    
</script> 
<script type="text/javascript">
    $(function(){
        var btnUpload=$('#upload_button');
        var status=$('#status_message');
        new AjaxUpload(btnUpload, {
            action: '<?=base_url()?>index.php/ajax/uploader/',
            name: 'uploadfile',
            onSubmit: function(file, ext){
                 if (! (ext && /^(jpg|png|jpeg|gif|)$/.test(ext))){ 
                    // extension is not allowed 
                    status.text('File cho phép Upload JPG, PNG, JPEG, GIF');
                    return false;
                }
                status.text('Đang tải File lên. Vui lòng đợi');
            },
            onComplete: function(file, response){ 
                
                //On completion clear the status
                status.text('');
                //Add uploaded file to list
                if(response!="error"){
                var str = response;
                var exploded = str.split(',');
                    $('<li></li>').appendTo('#list_img').html('<div  align="center"><img  onclick="chosen('+exploded[0]+')" src="'+urlsite+'uploads/images/'+exploded[1]+'" width="85" height="85"><a href="javascript:;" id="'+exploded[0]+'" class="delete_button"> Xóa</a><span id="v'+exploded[0]+'" style="display: none;">'+exploded[1]+'</span></div>').addClass('success');
                } else{
                    $('<li></li>').appendTo('#list_img').text(file).addClass('error');
                }
            }
        });
        
    });
</script> 
<script type="text/javascript">
$(function() {
	$(".delete_button").click(function() {
		if(confirm("Bạn có chắc chắn muốn xóa hình ảnh này\nĐồng ý chọn: OK\nHủy chọn: Cancel")){
			var id = $(this).attr("id");
			var dataString = 'id='+ id ;
			var parent = $(this).parent();
			
			$.ajax({
				type: "POST",
				url: url+"product/delimg",
				data: dataString,
				cache: false,
				beforeSend: function(){
					parent.animate({'backgroundColor':'#fb6c6c'},300).animate({ opacity: 0.35 }, "past");;
				}, 
				success: function(){
					//parent.slideUp('slow', function() {$(this).remove();});
				}   
			});
		}
		return false;
	});
});
function chosen(id)
{
	if(id)
	{
		var vl = $('#v'+id).text();
		$('#ProductImg').val(vl);
		$('#main_img').html('<img src="' + urlimg + vl + '" width="85" height="85" />');
	}
	else
	{
		return false;
	}
}     
</script> 
